<template>
	<view>
		
		<view class="item" v-for="(item,index) in list" :key='index' v-if="type==2" @tap="toDetail(item)" >
			<view class="avatar">
				<image :src="item.avatar" mode=""></image>
			</view>
			<view class="detail">
				<view class="title">
					{{ item.nickname }}
				</view>
				<view class="num">
					{{ doreset(item.createtime) }}
				</view>
			</view>
		</view>
		
		<view class="item" v-for="(item,index) in list" :key='index' v-if="type==1" @tap="toDetail(item)" >
			<view class="avatar">
				<image :src="item.avatar" mode=""></image>
			</view>
			<view class="detail">
				<view class="title">
					{{ item.nickname }}
				</view>
				<view class="num">
					{{ doreset(item.createtime) }}
				</view>
			</view>
		</view>
		
		<view class="item" v-for="(item,index) in list" :key='index' v-if="type==0" @tap="toDetail(item)" >
			<view class="avatar">
				<image :src="item.avatar" mode=""></image>
			</view>
			<view class="detail">
				<view class="title">
					{{ item.nickname }}
				</view>
				<view class="num">
					{{ doreset(item.createtime) }}
				</view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token'])
		},
		data() {
			return {
				page:1,
				pages:null,
				list:[],
				type:''
			};
		},
		//下拉刷新
		onPullDownRefresh() {
			console.log('下拉刷新');
			setTimeout(() => {
				uni.stopPullDownRefresh();
				this.page = 1;
				this.list = [];
				this.getData();
			}, 1000)
		},
		//上拉加载
		onReachBottom() {
			console.log('触发上拉加载');
			if (this.page < this.pages) {
				this.page++;
				this.getData();
			} else {
				uni.showToast({
					title: '到底了!',
					icon: 'none'
				})
			}
		},
		onLoad(options) {
			if(options.type==0){
				this.type = options.type;
				uni.setNavigationBarTitle({
					title:'我关注的'
				})
				
				//获取 数据
				this.getData();
				
			}else if(options.type==1){
				this.type = options.type;
				uni.setNavigationBarTitle({
					title:'谁关注我'
				})
				
				//获取 数据
				this.getData1();
				
			}else if(options.type==2){
				this.type = options.type;
				uni.setNavigationBarTitle({
					title:'最近访客'
				})
				
				//获取 数据
				this.getData2();
				
			}
			
		},
		methods:{
			//点击前往详情
			toDetail(e){
				console.log(e);
				// fromuserid userid
				let type;
				e.gender==1?type=0:type=1;
				uni.navigateTo({
					url: '/pages/recommend/personal?type='+ type +'&id=' + e.id
				})
			},			
			//获取数据
			async getData() {
				try {
					let params = {
						token: this.token,
						page:1
					};
					let res = await this.$request.request.get('/api/attention/getAttention', params)
					console.log(res);
					if (res.code == 200) {
						this.list = this.list.concat(res.data.list);
						this.pages = res.data.pages;
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			async getData1() {
				try {
					let params = {
						token: this.token,
						page:1
					};
					let res = await this.$request.request.get('/api/attention/attentioned', params)
					console.log(res);
					if (res.code == 200) {
						this.list = this.list.concat(res.data.list);
						this.pages = res.data.pages;
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			async getData2() {
				try {
					let params = {
						token: this.token,
						page:1
					};
					let res = await this.$request.request.get('/api/user/visit', params)
					console.log(res);
					if (res.code == 200) {
						this.list = this.list.concat(res.data.list);
						this.pages = res.data.pages;
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			//转换时间戳
			doreset(e) {
				//转换时间戳
				if ((e + '').length != 13) {
					var date = new Date(e * 1000); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
					var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
					var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
					var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				} else {
					var date = new Date(e); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
					var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
					var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
					var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
				}
				return Y + M + D + h + m + s;
			}
		}
	}
</script>

<style lang="scss">
@import 'follow.scss';
</style>
